<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_alizhima/model')->方法名();
	//小程序支付唤起预授权：https://opendocs.alipay.com/mini/api/openapi-pay?pathHash=e964f41e
	//芝麻免押：https://opendocs.alipay.com/open/repo-01jpyl
*/
namespace jy_alizhima;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	//取消
	public function cancel_xinyong($ordersn='',$aliapp_id=0,$isjie=0,$auth_no='',$tid=''){
		if(!$tid){
			$order=pdo_get('jy_weishop_order',array('ordersn'=>$ordersn,'uniacid'=>$_SESSION['uniacid']));
			if(!$order['id']){
				exi('订单不存在！','error');
			}
			$api_data=json_decode($order['api_data'],true);
			if($api_data['cancel_xinyong']){
				exi('该订单已解除过！','error');
			}
			$pay_log=pdo_get('core_paylog',array('id'=>$order['pay_id'],'uniacid'=>$_SESSION['uniacid']));
		}else{
			$pay_log=pdo_get('core_paylog',array('tid'=>$tid,'uniacid'=>$_SESSION['uniacid']));
		}
		if($pay_log['oldfee']<=0){
			exi('该订单没有押金！','error');
		}
		if($pay_log['oldfee']>0){
			$params=array(
				'pay_id'=>$pay_log['id'],
				'title'=>$pay_log['title'],
				'fee'=>$pay_log['fee'],
				'ordersn'=>$pay_log['tid'],
				'success_url'=>$pay_log['success_url'],
				'error_url'=>$pay_log['error_url'],
				'auth_no'=>$auth_no,
			);
			$wxapp=pdo_get('core_account_wxapp',array('id'=>$aliapp_id,'uniacid'=>$_SESSION['uniacid']));
			$wxapp['api_data']=json_decode($wxapp['api_data'],true);
			$params['ali_service_id']=$wxapp['api_data']['ali_service_id'];
			if(!$params['ali_service_id']){
				exi('小程序未绑定信用服务ID！','error');
			}
			$params['yafee']=$pay_log['oldfee'];
			$setting=array(
				'alipay_appid'=>$wxapp['ali_appid'],
				'alipay_private_key'=>$wxapp['alipay_private_key'],
				'alipay_public_key'=>$wxapp['alipay_public_key'],
				'target_app_id'=>$wxapp['target_app_id'],
			);
			if(!$isjie){
				model('jy_alipay/alipay',$setting)->aliapp_cancel_xinyong($params);
			}else{
				model('jy_alipay/alipay',$setting)->xinyong_jiedong($params);
			}
			$api_data['cancel_xinyong']=1;
			pdo_update('jy_weishop_order',array('api_data'=>ijson_encode($api_data)),array('id'=>$order['id'],'uniacid'=>$_SESSION['uniacid']));
		}
		return true;
	}
}
?>